<template>
    <div>
        <a-modal
            :width="765"
            title="签章"
            :visible="signatureVisible"
            @ok="signatureVisible = false"
            @cancel="signatureVisible = false"
            :forceRender="true"
            class="signature-modal-v"
        >
            <a-spin size="large" :spinning="signatureSpinning">
                <iframe 
                    src="/XTX_DEMO.html" 
                    ref="iframeOne" 
                    scrolling="auto" 
                    width="100%" 
                    :height="(windowInnerHeight -150 )+'px'"
                    style="border: 0;"
                ></iframe>
                <div style="display: flex;justify-content: center;" v-if="GXCADataLength">
                    <a-button :disabled="!GXCADataLength" style="margin-top: 20px;" class="blue-btn"  type="primary" @click.stop="signatureOk" >签章</a-button>
                </div>
            </a-spin>
        </a-modal>
    </div>
</template>
<script>
export default {
    components:{

    },
    props:{

    },
    data(){
        return{
            signatureVisible:false,
            signatureSpinning:false,
            GXCADataLength:false,
            selectFile:[],
            signatureData:{},
            sealSelect:'',//签章下拉选中的值
            signatureDataArr:[],//签章数据
            signatureresultArr:[],//签章结果
            signatureSpinning:false,
        }
    },
    watch:{

    },
    computed: {
        GXCADataLength(){
            if(this.$store.state.permission.GXCAData.length == this.selectFile.length){
                this.signatureSpinning = false
            }
            return this.$store.state.permission.GXCAData.length == this.selectFile.length
        },
    },
    mounted(){
        window.addEventListener("message", this.handleMessage);
    },
    methods:{
        getSelectFile(val){
            this.selectFile = val
        },
        handleMessage(val){
            var that = this
            if(val.data.params.certSn){
                this.signatureSpinning = true
                this.signatureData.certSn = val.data.params.certSn
                getSealList({certSn:val.data.params.certSn}).then(res=>{
                    if(res.code == 200){
                        this.$refs.iframeOne.contentWindow.postMessage({
                            params: {sealList:res.result.sealList} /*在iframe页面中接收通过key也就是param接收，因此传输的数据可以是对象，包含多个key以及对应的数据*/
                        }, "*");
                        
                    }else{
                        this.$message.warning(res.message)
                    }
                })
            }else{
                this.signatureDataArr = []
                this.signatureresultArr = [] 
            }
            if(val.data.params.sealSelect){
                //console.log('签章类型',val.data.params.typeSelectValue)
                var typeValue = {} 
                if(val.data.params.typeSelectValue){
                    typeValue[val.data.params.typeSelectValue] = true
                }
                this.$store.commit("SET_GXCADATA_NULL", [])
                this.signatureSpinning = true
                this.sealSelect = val.data.params.sealSelect
                this.signatureData.sealSelect = val.data.params.sealSelect
                for(let i = 0; i <= this.selectFile.length;i++){
                    (function(i) {
                        setTimeout(() =>{
                            if(that.selectFile[i]){
                                sortJson({
                                    certSn:that.signatureData.certSn,
                                    ids:that.selectFile[i],
                                    seal:val.data.params.sealSelect,
                                    ...typeValue,
                                }).then(res2=>{
                                    if(res2.code ==200){
                                        that.$refs.iframeOne.contentWindow.postMessage({
                                            params: {sealData:res2.result.data} 
                                        }, "*");
                                    }else{
                                        that.$message.warning(res2.message)
                                    }
                                })
                            }
                            
                        },3000);
                    })(i)
                }
                
            }
            if(val.data.params.signinfo){
                this.$store.commit("SET_GXCADATA", {data:val.data.params.sealData,signinfo:val.data.params.signinfo})
            }
        },
        signatureOk(){

        }
    }
}
</script>